﻿using System;
using System.IO;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.ReportSource;
using CrystalDecisions.Shared;
using DevExpress.Skins;
using DevExpress.LookAndFeel;
using DevExpress.UserSkins;
using DevExpress.XtraBars;
using DevExpress.XtraBars.Ribbon;
using DevExpress.XtraBars.Helpers;
using CNotebook;

namespace NotebookS2
{
    public partial class PRNT_WTS : RibbonForm
    {
        public PRNT_WTS()
        {
            InitializeComponent();
        }
        public string ConnectionString { get; set; }
        public string DocumentNumber { get; set; }
        public string DocumentStatus { get; set; }

        public string GetLayout()
        {
            ObjectTypeManager oManager = new ObjectTypeManager();
            const int _DOCUMENT_TYPE = 103;
            oManager.ConnectionString = this.ConnectionString;

            oManager.Open();

            string _FILE_NAME = oManager.GetPrintLayout1(_DOCUMENT_TYPE);

            oManager.Close();

            return _FILE_NAME;
        }

        //public string GetLayout()
        //{
        //    string sPath = System.Windows.Forms.Application.StartupPath.ToString() + "\\config.conf";
        //    string _OUTPUT_STRING = "";

        //    if (File.Exists(sPath) == true)
        //    {
        //        StreamReader sr = File.OpenText(sPath);
        //        string input;

        //        do
        //        {
        //            input = sr.ReadLine();
        //            if (input != null)
        //            {

        //                if (input.StartsWith("WTSLayout") == true)
        //                {
        //                    int iStart = input.IndexOf("=");
        //                    _OUTPUT_STRING = input.Substring(iStart + 1);

        //                    break;
        //                }

        //            }
        //        } while (input != null);


        //    }
        //    return _OUTPUT_STRING;

        //}


        public void ViewWTSLayout(string sDocNumber)
        {
            ReportDocument rptDoc = new ReportDocument();
            SqlConnection oConnection = new SqlConnection();
            SqlCommand oCommand = new SqlCommand();
            SqlDataAdapter oAdapter = new SqlDataAdapter();
            DataSet ds = new DataSet();

            try{
            string sReportFile = "";

                sReportFile = GetLayout();
            
            rptDoc.Load(sReportFile);


            
            this.DocumentNumber = sDocNumber;

            oConnection.ConnectionString = this.ConnectionString;
            oConnection.Open();

            oCommand.Connection = oConnection;
            oCommand.CommandType = CommandType.StoredProcedure;
            oCommand.CommandText = "SP_LAYOUT_WTS";

            oCommand.Parameters.Add(new SqlParameter("@DocNum", sDocNumber));
            oAdapter.SelectCommand = oCommand;
            oAdapter.Fill(ds);

            oConnection.Close();


            rptDoc.SetDataSource(ds.Tables[0]);
            CRViewer.ReportSource = rptDoc;
            CRViewer.Refresh();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            finally
            {
                rptDoc.Dispose();
                oConnection.Dispose();
                oCommand.Dispose();
                oAdapter.Dispose();
                ds.Dispose();
            }
        }

        private void PRNT_PO_Load(object sender, EventArgs e)
        {
            ViewWTSLayout(this.DocumentNumber);
        }

        private void CRViewer_Load(object sender, EventArgs e)
        {

        }
    }
}
